import acttoolv2 from '../toolv2.js';
//目前没有用到过


var actmodel = {
  //加载定位
  mylocation: function (cache) {
    if(!cache && cache!==0){
      cache =300  //默认缓存5分钟
    }
    return new Promise(resolve => {
      var app = getApp();

      var vtimestamp = acttoolv2.timestamp()
      var location = app.globalData.userlocation.location
      if (cache *1000 - (vtimestamp - location.lasttime) > 0) {
    

        resolve({status:1,obj:location} )
      }
      else{
      
        acttoolv2.forcegetLocation(false).then(function(res){
          console.log("forcegetLocation")
          console.log(res)

          if(res["status"]==1){
            location["lasttime"]=vtimestamp;
            location["latitude"]=res["latitude"]
            location["longitude"] =res["longitude"]    
          }
          resolve(res)

        } )
      }
    })
  },
/**
 * 加载地址
 */
  lastaddr:function(cache){   
 

    if(!cache)
    {
      cache = 300
    }
    return new Promise(resolve=>{
 
      var app =getApp();
      var vtimestamp = acttoolv2.timestamp()
      var lastaddr = app.globalData.userlocation.lastaddr
      if (cache *1000 - (vtimestamp - lastaddr.lasttime) > 0) {
       
        resolve({status:1,obj:lastaddr} )
      }
      else
      {
       
        actmodel.mylocation().then(function(locationreslut){ 
          console.log("locationreslut")
          console.log(locationreslut)

          if(locationreslut["status"]==1){
           
            var location =locationreslut["obj"]
            acttoolv2.locationotaddr({lat:location.latitude,lng:location.longitude},1,0).then(function(addr){
             
              if(addr.status==-1){
                resolve(addr)
              }else{
                addr["lasttime"] =vtimestamp           
                app.globalData.userlocation.lastaddr =addr;
                resolve({status:1,obj:addr})
              }
              
             
            })
          }
          else
          {      
              
              resolve(locationreslut)        
          }  
  
        })  
      }
    })    
  },
  btngetLocation: function () {

    wx.getSetting({
      success(res) {
        if (res.authSetting["scope.userLocation"] === false) {
          wx.showModal({
            title: '获取附近需求授权使用地理位置',
            content: '获取附近需求授权使用地理位置',
            success(res) {
              if (res.confirm) {
                wx.openSetting({
                  success(res) {
                    if (res.authSetting["scope.userLocation"] == false) {
                      //定位失败
                      resolve({ status: -1, message: "定位失败,未授权", position: { lng: -1, lat: -1 } })

                    } else {
                      actmodel.getLocation().then(function (a) {
                        resolve(a)
                      })
                    }
                  },
                  fail(res) {

                    resolve({ status: -1, typcode: "", message: "定位失败,未授权" })
                  }
                });
              } else if (res.cancel) {
                resolve({ status: -1, typcode: "", message: "定位失败,未授权" })
              }
            }
          })
        }
        else {
          actmodel.getLocation().then(function (a) {
            resolve(a)
          })
        }
      }
    })
  },
  getLocation: function (cse) {
    var app = getApp();
    return new Promise(resolve => {
      var userlocation = app.globalData.userlocation
      if (userlocation.location.code == "none") {
        userlocation.location.code = "ing";
        // this.startLocation();
        resolve({ status: 1, message: "定位中" })
      }
    })
  },

  //开启周期自动获取定位
  startLocation: function () {
    // wx.onLocationChange(function (res) {
    //   var app = getApp();
    //   var location = app.globalData.userlocation.location
    //   var fields = ["latitude", "longitude", "speed", "accuracy", "altitude", "verticalAccuracy", "horizontalAccuracy"]
    //   location.lasttime = acttoolv2.timestamp()
    //   fields.forEach(item => {
    //     location[item] = res[item];
    //   })

    // })
    wx.offLocationChange(function () {
      app.globalData.userlocation.location.code = "none"
    })
    // wx.startLocationUpdateBackground({
    //   complete(erx) {
    //     if (erx["errMsg"] == 'startLocationUpdateBackground:fail auth deny') {
    //       var app = getApp();
    //       app.globalData.userlocation.location.code = "deny"
    //     }
    //   }
    // })
  }
}
module.exports = actmodel;